package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import com.haoyeteng.sxbd.core.exception.SxbdException;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.model.Account;
import com.haoyeteng.sxbd.modular.business.model.News;
import com.haoyeteng.sxbd.modular.business.service.INewsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.haoyeteng.sxbd.modular.business.model.Category;
import com.haoyeteng.sxbd.modular.business.service.ICategoryService;

/**
 * 新闻分类表控制器
 *
 * @author haoyeteng
 * @Date 2019-06-11 21:18:56
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private String PREFIX = "/business/category/";

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private INewsService newsService;

    /**
     * 跳转到新闻分类表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }

    /**
     * 跳转到添加新闻分类表
     */
    @RequestMapping("/category_add")
    public String categoryAdd() {
        return PREFIX + "category_add.html";
    }

    /**
     * 跳转到修改新闻分类表
     */
    @RequestMapping("/category_update/{categoryId}")
    public String categoryUpdate(@PathVariable Integer categoryId, Model model) {
        Category category = categoryService.selectById(categoryId);
        model.addAttribute("item",category);
        LogObjectHolder.me().set(category);
        return PREFIX + "category_edit.html";
    }

    /**
     * 获取新闻分类表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(String categoryName) {
        Page<Category> page = new PageFactory<Category>().defaultPage();
        List<Map<String, Object>> categories = categoryService.selectCategories(page,categoryName);
    	ResponseResult<Object> result = new ResponseResult<Object>();
        result.setData(categories);
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     * 新增新闻分类表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(Category category) {
        Date date = new Date();
        category.setCreateTime(date);
        category.setUpdateTime(date);
        category.setFlag(1);
        categoryService.insert(category);
        return SUCCESS_TIP;
    }

    /**
     * 删除新闻分类表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission
    public Object delete(@RequestParam Integer categoryId) {
        List<News> categories = newsService.selectList(new EntityWrapper<News>().eq("category_id", categoryId));
        if (ToolUtil.isNotEmpty(categories)) {
            throw new SxbdException("该分类已被新闻引用，不能被删除");
        }else {
            categoryService.deleteById(categoryId);
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改新闻分类表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(Category category) {
        category.setUpdateTime(new Date());
        categoryService.updateById(category);
        return SUCCESS_TIP;
    }

    /**
     * 新闻分类表详情
     */
    @RequestMapping(value = "/detail/{categoryId}")
    @ResponseBody
    public Object detail(@PathVariable("categoryId") Integer categoryId) {
        return categoryService.selectById(categoryId);
    }
}
